package com.hdoit.ares.web.share.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.share.model.ShareInfo;
import com.hdoit.ares.logical.share.model.ViewTime;
import com.hdoit.ares.logical.share.service.IShareInfoService;
import com.hdoit.ares.logical.viewmodel.share.ShareInfoSearch;
import com.hdoit.ares.selfcommon.enums.share.ViewTimeState;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;


/**
 * @author      wangxuexue
 * @email       
 * @createtime  2017年11月18日 13:50:00
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/share/shareInfo/")
public class ShareInfoController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/share/shareInfo/";
    }

    private IShareInfoService shareInfoService;
    public IShareInfoService getShareInfoService() {
        return shareInfoService;
    }
    @Resource
    public void setShareInfoService(IShareInfoService shareInfoService) {
        this.shareInfoService = shareInfoService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="shareInfoDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ShareInfoSearch shareInfoSearch,boolean ajaxPost){

        pagerModel.setTotalAmount(shareInfoService.queryShareInfoCount(shareInfoSearch));
        List<ShareInfo> shareInfos= shareInfoService.queryShareInfoList(shareInfoSearch,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("shareInfos", shareInfos);
        model.addAttribute("shareInfoSearch", shareInfoSearch);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String shareInfoDetailed(Model model,String key){
        ShareInfo shareInfo=new ShareInfo();
        if(!StringUtils.isEmpty(key)){
            shareInfo=shareInfoService.queryShareInfoModel(key);
        }else if(StringUtils.isEmpty(key)){
        	ViewTime viewTime=new ViewTime();
        	viewTime.setType(ViewTimeState.info);
        	shareInfo.setViewTimes(viewTime);
        }
        model.addAttribute("shareInfo", shareInfo);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,ShareInfo shareInfo){
        if(StringUtils.isEmpty(shareInfo.getId())){
            shareInfo.setId(CommonUtils.getKeyId());
            shareInfo.setCreateDate(DateUtils.getCurrent());
            shareInfo.setDeleteMark(YesOrNo.no);
            shareInfo.setCreateBy(getLoginModel().getLoginName());
            shareInfo.setModelStatus(ModelStatus.add);
        }else{
            shareInfo.setModifyDate(DateUtils.getCurrent());
            shareInfo.setModifyBy(getLoginModel().getLoginName());
            shareInfo.setModelStatus(ModelStatus.update);
        }

        shareInfo.setCode(ChineseUtils.getPingYin(shareInfo.getInfoTitle()));
        shareInfo.setMarkCode(ChineseUtils.getPinYinHeadChar(shareInfo.getInfoTitle()));
        
        ActionResult actionResult;
        if(shareInfoService.applyChange(shareInfo)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("shareInfo", shareInfo);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }
    
    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(shareInfoService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
